// pages/managerList/managerList.js
import config from "../../config.js";
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: undefined,
    isChangeDefault: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var isChangeDefault = options.isChangeDefault;
    if (isChangeDefault != null) {
      that.setData({
        isChangeDefault: true
      })
    }
    wx.showLoading({
      title: '请稍等',
    })
    wx.request({
      url: base.houseKepperList,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        start: 0,
        length: 10,
        search: ''
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res);
        if (res.statusCode == 200) {
          var list = [];
          for (let i = 0; i < res.data.data.length; i++) {
            var item = res.data.data[i];
            var star = [];
            for (var j = 0; j < item.score; j++) {
              star.push(j);
            }
            item['star'] = star;
            console.log(item);
            list.push(item);
          }
          that.setData({
            list: list
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  tapAction: function(e) {
    var keeperId = e.currentTarget.dataset.data.keeperId;
    console.log(e);
    var that = this;
    if (that.data.isChangeDefault) {
      wx.showModal({
        title: "温馨提示",
        content: "是否确认更换专属车管家？",
        showCancel: true,
        confirmColor: "#137AF0",
        success: function(er) {
          if (er.confirm) {
            that.changDefaultKeeper(keeperId);


          }
        }
      })

    } else {

      wx.setStorageSync("keeperId", e.currentTarget.dataset.data.keeperId);
      wx.setStorageSync("houseKeeper", e.currentTarget.dataset.data);

      wx.navigateBack({})
    }


  },
  changDefaultKeeper: function(keeperId) {
    var that = this;

    wx.showLoading({
      title: '数据提交中...',
    })
    wx.request({
      url: base.changeMyHouseKepper,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: keeperId,
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res);
        if (res.data.status == 200) {
          wx.setStorageSync("keeperId", keeperId);
          wx.setStorageSync("MyDefaultHouseKeeper", res.data.data);
          wx.setStorageSync("houseKeeper", res.data.data);
          var user = wx.getStorageSync("user");
          user.housekeeper = res.data.data;
          wx.setStorageSync("user", user);

          wx.navigateBack({})
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  }
})